package cn.kzgg.org.controller;

import cn.kzgg.basic.exception.BusinessException;
import cn.kzgg.basic.util.AjaxResult;
import cn.kzgg.basic.util.ExcelUtil;
import cn.kzgg.basic.util.PageList;
import cn.kzgg.org.domain.Shop;
import cn.kzgg.org.domain.ShopAuditLog;
import cn.kzgg.org.query.ShopQuery;
import cn.kzgg.org.service.IShopService;
import cn.kzgg.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;





    /**
     * 导出报表
     * @param id
     * @return
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xls",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入
     * @param response
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        System.out.println("一共导入"+shops.size()+"行");
    }

    /**
     * 报表展示
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo> voList = shopService.queryEcharts();
        List<Integer> states = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> nums = voList.stream().map(ShopVo::getNum).collect(Collectors.toList());
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("x",states);
        hashMap.put("y",nums);
        return hashMap;
    }



    /**
     * 驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            System.out.println(shopAuditLog);
            return AjaxResult.show();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }


    /**
     *通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.show();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }


    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.show();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.removeById(id);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> list() {

        return shopService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }
}
